<template>
  <div class="login">
    <div class="box">
      <div class="title">火星</div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="userName">
          <el-input
            prefix-icon="el-icon-user"
            v-model="ruleForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input
            prefix-icon="el-icon-lock"
            show-password
            v-model="ruleForm.userPwd"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width:100%;" type="primary" @click="doLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import loginApi from '@/api/login'
export default {
  data() {
    return {
      ruleForm: {
        userName: "admin",
        userPwd: "123456",
      },
      rules: {
          userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          userPwd: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ],
      },
    };
  },
  mounted() {},
  methods: {
      doLogin(){
          this.$refs['ruleForm'].validate(async (valid)=>{
              if(valid){
                  let res = await loginApi.doLogin(this.ruleForm);
                //   console.log(res);
                  this.$store.commit('saveToken', res.token);
                  this.$store.commit('saveUserInfo', res);
                  this.$message.success({
                      message: '登录成功',
                      offset: 80
                  })
                  this.$router.push('/layout')
              }else{
                  this.$message.error({
                      message: '用户名或密码不能为空',
                      offset: 80,
                  })
              }
          })
      }
  },
};
</script>

<style scoped lang='scss'>
.login {
  width: 100vw;
  height: 100vh;
  background-color: rgb(234, 250, 250);
  display: flex;
  align-items: center;
  justify-content: center;
  .box {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    box-shadow: 0 0 10px 5px rgb(233, 230, 230);
    border-radius: 5px;
    .title {
      font-size: 50px;
      text-align: center;
      margin-bottom: 20px;
    }
  }
}
</style>
